
import UserManager.usermanager;
import configuration.config_errors;
import configuration.config_manager;
import java.io.IOException;
import java.util.logging.*;
import javax.swing.JOptionPane;
import postmaster.postmaster;
import postmaster.postmaster_errors;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author maniksin
 */
public class main {

    public static Logger logger;   
    
    
    /*
    private static final int SERVICE_MODE = 0;
    private static final int CLIENT_MODE = 1;
    private static final int UNKNOWN_MODE = 2;
    */
    
    /**
     * @param args the command line arguments
     */
    @SuppressWarnings("ResultOfObjectAllocationIgnored")
    
    
    public static void main(String[] args) {
        
        config_manager config;
        config_errors err_num;
        postmaster pm;
        
        // Initialize logger
        logger = Logger.global;
        logger.setLevel(Level.CONFIG);
        
        String config_path = System.getenv("TRANSPORTER_CONFIG");
        if (config_path == null) {
            config_path = "transporter.config";
        }
        
        config = new config_manager(config_path);
        err_num = config.config_state();
        if (err_num != config_errors.SUCCESS) {
            // Could not read configuration
            JOptionPane.showMessageDialog(null, "Failed to read configuration (TRANSPORTER_CONFIG)", err_num.toString(), 
                    JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        pm = new postmaster(config);
        if (pm.is_ready() == postmaster_errors.SUCCESS) {
            // Bind successful!! We are the master.
            // We must not be invoked as a client to master
            if (args.length != 0) {
                // Arguments are passed only to the client.
                
                // Do not raise a dialog box. We would be probably invoked from
                // a script. Dialog box would block the script.
                System.err.println("TRANSPORTER_ACK: postmaster not running");
                pm.close_postmaster();
                return;
            }
            
            // Set log file
            String logfile = config.log_file;
            try {
                Handler fh = new FileHandler(logfile, false);
                fh.setFormatter(new SimpleFormatter());
                logger.addHandler(fh);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Failed to initialize log file", "IOException", 
                        JOptionPane.ERROR_MESSAGE);
                return;
            } catch (SecurityException ex) {
                JOptionPane.showMessageDialog(null, "Failed to initialize log file", "Security Exception", 
                        JOptionPane.ERROR_MESSAGE);
                return;
            }          
            
            
            pm.start_postmaster();
            logger.warning("Postmaster exited!!");
        } else {
            
            // If we are run in master mode, raise an error
            if (args.length == 0) {
                // Args are not provided in master mode
                JOptionPane.showMessageDialog(null, "Could not bind to port", "Transporter error", 
                        JOptionPane.ERROR_MESSAGE);
            } else {
                //System.out.println("Num args received: " + args.length);
                new usermanager(args, pm.conf);
            }
        }
    }
}
